package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.ICompanyService;
import com.cn.wanxi.util.IOTool;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class CompanyServiceImpl implements ICompanyService {
    private String split = "\n";

    @Override
    public CompanyModel getCompanyModel() {
        String path = "D:\\wanxi\\step-3\\canyin-03\\web\\note";
        String fileName = "company.txt";
//创建文件
        IOTool.createNote(path, fileName);
//写入数据
        List<String> data = setCompanyData();
        String result = IOTool.writeData(data);
//        if (result.equals("error")) {
//            return null;
//        }
//读取数据
        String string = IOTool.readData();
//封装数据
        CompanyModel companyModel = setData(string);
        return companyModel;
    }

    private List<String> setCompanyData() {
        CompanyDao companyDao = new CompanyDao();
//        String[] data = {companyDao.logo, companyDao.phone, companyDao.net,companyDao.linePhone,companyDao.email,companyDao.address,companyDao.version};
        List<String> list = new ArrayList<>();
        list.add(companyDao.logo);
        list.add(companyDao.phone);
        list.add(companyDao.linePhone);
        list.add(companyDao.net);
        list.add(companyDao.address);
        list.add(companyDao.email);
        list.add(companyDao.version);
        return list;
    }

    /**
     * 根据读取出来的结果封装成companyModel
     *
     * @param string
     * @return
     */
    private CompanyModel setData(String string) {
        String[] split = string.split(this.split);
        System.out.println("---------------要封装的数据-------------------");
        for (int i = 0; i < split.length; i++) {

            System.out.println(split[i] + "----" + i);
        }
        CompanyModel companyModel = new CompanyModel();
        try {
            companyModel.setLogo(split[0]);
            companyModel.setEmail(split[1]);
            companyModel.setAddress(split[2]);
            companyModel.setPhone(split[3]);
            companyModel.setLinePhone(split[4]);
            companyModel.setNet(split[5]);
            companyModel.setVersion(split[6]);
        } catch (Exception e) {
        }

        return companyModel;
    }

    /**
     * 创建文件夹
     */
//    private File createNote() {
//        String path = "D:\\wanxi\\step-3\\canyin-03\\web\\note";
//        String fileName = "company.txt";
//        File directory = new File(path);
//        if (!directory.exists()) {
//            directory.mkdirs();    //创文件夹
//        }
//        File file = new File(path, fileName);
//        try {
//            file.createNewFile();  //创文件
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return file;
//    }

    /**
     * 写入数据
     */
//    private String writeData(File note) {
//        //参数校验
//        if (note == null || !note.exists()) {
//            return "error";
//        }
//        try {
//            OutputStream outputStream = new FileOutputStream(note);
////            char sqw = '孙';
////            Character sqwww = sqw;
//            CompanyDao companyDao = new CompanyDao();
//
////            outputStream.write(sqwww.toString().getBytes());
//            outputStream.write(companyDao.logo.getBytes());
//            outputStream.write(split.getBytes());
//            outputStream.write(companyDao.email.getBytes());
//            outputStream.write(split.getBytes());
//            outputStream.write(companyDao.address.getBytes());
//            outputStream.write(split.getBytes());
//            outputStream.write(companyDao.phone.getBytes());
//            outputStream.write(split.getBytes());
//            outputStream.write(companyDao.linePhone.getBytes());
//            outputStream.write(split.getBytes());
//            outputStream.write(companyDao.net.getBytes());
//            outputStream.write(split.getBytes());
//            outputStream.write(companyDao.version.getBytes());
//            outputStream.write(split.getBytes());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return "success";
//    }


//    public static void main(String[] args) {
//        new CompanyServiceImpl().getCompanyModel();
//    }


}
